package com.interview.three;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.support.GenericApplicationContext;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 学习SpringBoot的循环依赖
 * Setting 方法注入
 */
@Slf4j
public class TestSeterCircularDependency {

    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("a",A.class);
        context.registerBean("b",B.class);
        context.registerBean(MyAspectT.class);
        context.registerBean(AnnotationAwareAspectJAutoProxyCreator.class);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(context.getDefaultListableBeanFactory());
        context.refresh();
        
        context.getBean(A.class).fooA();
    }
    @Slf4j
    static class A{
        private B b;

        public A() {
            log.info("A()");
        }
        @Resource
        public void setB(B b) {
            log.info("依赖注入B+{}",b.getClass());
            this.b = b;
        }
        @PostConstruct
        public void init(){
            log.error("A初始化....");
        }
        public void fooA(){
            log.error("A类中的foo方法-------");
        }
    }
    @Aspect
    static class MyAspectT{
        @Before("execution(* fooA())")
        public void before(){
            log.info("before ..--...");
        }
    }
    @Slf4j
    static class B{
        private A a;

        public B() {
           log.info("B()");
        }

        @Resource
        public void setA(A a) {
            log.info("依赖注入A+{}",a.getClass());
            this.a = a;
        }
        @PostConstruct
        public void init(){
            log.error("B初始化-----");
        }
    }
}
